<div id="basket">
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <?php
    // REICH - 26.08.2010
    // Set when called from review, to handle output of certain buttons / divs
    $readonly = $_SESSION['readonly'];
    //Main Basket class integration - MOVED TO HEADER.INC
    //-----------------------------
//	Basket::UnsetVars();
//	$additem						= false;
//	$action							= Basket::GetAction($_REQUEST);
//	if($action) { Basket::Process(); }
    //------------------------------------------------------------

    $basket_sql				= Basket::SQL();
    $array				= Basket::CheckoutArray();
    $basket_db_resource_items		= DB::query($basket_sql['accessory']);

    // REICH - 02.08.2010 - Checkout Logic
    //if(array_key_exists('checkout', $_REQUEST)) {
    //    $checkout = true;
    //} else {
    //    $checkout = false;
    //}
    
//echo "<p>action: ". $action . "</p>";
//echo "<p>basket_sql: ". print_r($basket_sql) . "</p>";
//echo "<p>array: ". print_r($array) . "</p>";
//echo "<p>basket_db_resource_items: ". print_r($basket_db_resource_items) . "</p>";
    //-----------------------------
    //-----------------------------

    //set image checker backup (currently uses $loc to turn relative into absolute)
    $ImageChecker->setBackupImage($loc . "images/staticbackups/notfound146x106.png");

    //set main basket variables
    //-----------------------------
    $basket_summary_array				= Basket::Summary();
//echo "<p>basket_summary_array: ". print_r($basket_summary_array) . "</p>";
    $basket_total_num_items 			= $basket_summary_array['quantity'];
    $basket_total_num_text 				= " Artikel";
    if ($basket_total_num_items > 1) $basket_total_num_text. "s";		//add an 's' to the text if more than one item.
    $basket_items_price_total			= 0;		//might be calulated here when going through the items
    $basket_items_price_saving			= 0;		//same as the total, might be calculated through items loop
    $basket_items_max_quantity			= 5;
    //-----------------------------
    //function defaults
    //-----------------------------
    $basket_function_desc_separator		= "****";	//CostBiters seperation string in ExtendedDesc
    $basket_function_desc_limit			= 180;	//average search index to use to show an inital amount of basket text
    //-----------------------------

    ?>
    <?php if($readonly==false) { ?>
    <div id="basket_header_summary">
        <div id="basket_header_summaryinner">
            <div id="basket_header_summarytitle"><?=Language::Get('basket',0)?></div>
            <div id="basket_header_summaryinfo">Sie haben <strong><?php echo $basket_total_num_items . $basket_total_num_text; ?> in Ihrem Warenkorb.</strong> Siehe unten:</div>
            <div id="basket_header_summarydelivery"><?=Language::Get('basket',1)?></div>
        </div>
    </div>
    <?php } //readonly ?>
    <div class="basket_separator"></div>
    <table id="basket_items" cellpadding="0" cellspacing="0" border="0">
        <tr class="basket_items_headerrow">
            <th></th>
            <th class="left"><?=Language::Get('basket',2)?></th>
            <th class="left"><?=Language::Get('basket',3)?></th>
            <th><?=Language::Get('basket',4)?></th>
        </tr>
        <?php
        //cycle through each basket item
        //-----------------------------
        $basket_current_item_index = 0;
        while ($basket_current_item = DB::fetch($basket_db_resource_items)) {
//print_r($basket_current_item);
            $basket_current_item_index			+= 1;
            $basket_current_item_cartid			= $basket_current_item['CartID'];
            //grab info here that is needed in the table columns
            $basket_current_item_title			= utf8_encode($basket_current_item['AccessoryTitle']);
            //note the desc text includes the links and spans for more info
            $basket_current_item_desc			= basket_item_clean_description($basket_current_item['AccessoryDescription'], $basket_current_item_title, $basket_current_item_index);
            $basket_current_item_stockcode		= $basket_current_item['AccessoryStockCode'];
            $basket_current_item_imageelem		= $ImageChecker->getImg("/web3g/img/accessories/". $basket_current_item_stockcode .".jpg", "146", "106", $basket_current_item_title, "", "");
            $basket_current_item_pricerrp		= $basket_current_item['AccessoryRRP'];
            $basket_current_item_pricecurrent	= $basket_current_item['AccessoryPrice'];
            if ($basket_current_item_pricerrp > 0 && $basket_current_item_pricecurrent > 0) {	//if no offer, then set the saving to 0
                $basket_current_item_pricesaving	= round(100 * (1 - ($basket_current_item_pricecurrent / $basket_current_item_pricerrp)));
            } else {
                $basket_current_item_pricesaving = 0;
            }
            $basket_current_item_quantity		= $basket_current_item['Quantity'];
            if ($basket_current_item_pricecurrent > 0) {		//only the RRP may be set
                //$basket_items_price_total		+= $basket_current_item_pricecurrent * $basket_current_item_quantity;
                $basket_items_price_total		+= $basket_current_item_pricerrp * $basket_current_item_quantity;
                $basket_items_price_saving		+= ($basket_current_item_pricerrp - $basket_current_item_pricecurrent) * $basket_current_item_quantity;
            }
            else {
                $basket_items_price_total		+= $basket_current_item_pricerrp * $basket_current_item_quantity;
            }
    //-----------------------------
    ?>
        <tr>
            <td class="item_number"><?php echo $basket_current_item_index; ?>.</td>
            <td class="item_image">
    <?php echo $basket_current_item_imageelem; ?>
            </td>
            <td class="item_description">
                <h3><?php echo $basket_current_item_title; ?></h3>
                <p><?php echo $basket_current_item_desc; ?></p>
                <?php if($readonly==false) { ?><form id="basket_form_itemremove_<?php echo $basket_current_item_index; ?>" action="/basket/" method="post"><input type="hidden" name="esid" value="<?php echo $_SESSION['esid']; ?>" /><input type="hidden" name="remove" value="remove" /><input type="hidden" name="ctid" value="<?php echo $basket_current_item_cartid; ?>" /><a href="#" onclick="parentNode.submit()" class="item_description_remove">Aus dem Warenkorb entfernen</a></form><?php } //readonly ?>
            </td>
            <td class="item_price">    
            <br />
            <span class="price">&euro;<?php echo $basket_current_item_pricerrp; ?></span><br />
            <span class="saving"></span><hr width="80%" />
    <?=Language::Get('basket',5)?>
                <?php if($readonly==false) { ?>
                <form id="basket_form_itemquantity_<?php echo $basket_current_item_index; ?>" action="/basket/" method="post">
                    <input type="hidden" name="esid" value="<?php echo $_SESSION['esid']; ?>" />
                    <input type="hidden" name="ctid" value="<?php echo $basket_current_item_cartid; ?>" />
                <?php } //readonly ?>
                    <select name="quantity" onchange="parentNode.submit()" <?php if($readonly==true) echo 'disabled' ?>>

                            <?php
                            for($j = 1; $j <= $basket_items_max_quantity; $j++) {
                                if ($j == $basket_current_item_quantity) {
                                    echo "<option value=\"$basket_current_item_quantity\" selected>";
                                }
                                else {
                                    echo "<option value=\"$j\">";
                                }
                                echo $j . "</option>\n";
                            } ?>
                    </select>
                </form> 
            </td>
        </tr>        
        <tr class="basket_items_separator"><td colspan="4"></td></tr>
    <?php } ?>
    <?php if($readonly==false) { ?>
        <tr><td colspan="4" class="checkoutseparator"></td></tr>
        <tr>
            <td colspan="4" class="right">
                <span id="basket_saving_price"><?php if($basket_items_price_saving > 0) Language::Get('basket',6). "&euro;". sprintf("%1\$.2f", $basket_items_price_saving); ?>
                    <img src="/images/spacer.gif" width="40" height="10" />Total:</span>
                <span id="basket_total_price">&euro;<?php echo sprintf("%1\$.2f", $basket_items_price_total); ?></span>
                <img src="/images/spacer.gif" width="20" height="10" />
            </td>
        </tr>
        <tr><td colspan="4" class="checkoutseparator"></td></tr>
        <tr>
            <td colspan="2" class="basket_continueshopping"><a href="<?php echo $loc; ?>"><img src="/images/spacer.gif" class="spritebg continueshop" /></a></td>
            <td colspan="2" class="basket_checkout right">
<?php if($basket_total_num_items > 0) { ?>
                <form id="basket_form_submit" action="/billing/" method="post">
                    <?php $_SESSION["Payment_Amount"] = $basket_items_price_total; ?>
                    <!-- <input type="hidden" name="checkout" value="checkout" /> -->
                    <input type="image" src="/images/spacer.gif" class="spritebg checkout" />
                </form>
    <?php } ?></td>
        </tr>
        <?php } //readonly ?>
    </table>
    <?php if($readonly==false) { ?>
    <div class="basket_separator"></div>
        
    <form action='/expresscheckout/' METHOD='POST'>
        <?php $_SESSION["Payment_Amount"] = $basket_items_price_total; ?>
        <input type='image' name='submit' src='https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif' border='0' align='top' alt='Check out with PayPal'/>
    </form>
    <?php } //readonly ?>
</div>
<?php 
//basket related functions
function basket_item_clean_description($desc, $title, $index) {
    GLOBAL $basket_function_desc_separator;
    GLOBAL $basket_function_desc_limit;

    //first split the description since CostBiter are putting all data (desc,spec,features... etc) in ExtendedDesc and we only need the first section
    $separatorposition = strpos($desc, $basket_function_desc_separator);
    if ($separatorposition !== false) {
        $currentstring = substr($desc, 0, $separatorposition);
        //hack off the 2 page breaks also included at the end of the section string
        $currentstring = substr($currentstring, 0, (strlen($currentstring) - 12));
    }
    else {
        $currentstring = $desc;
    }	//no separator, just copy the string info

    //now insert a hiding span if applicable
    if (strlen($currentstring) > $basket_function_desc_limit) {
        $splitindex = strpos($currentstring, " ", $basket_function_desc_limit);
        $currentstring = substr($currentstring, 0, $splitindex) . "<span class=\"basket_item_hide\" id=\"basket_item_hide_". $index ."\">" . substr($currentstring, $splitindex, (strlen($currentstring) - $splitindex)) . "</span>";
        $currentstring .= "<a href=\"#\" onclick=\"document.getElementById('basket_item_hide_". $index ."').style.display = 'inline';document.getElementById('basket_item_hidelink_". $index ."').style.display = 'none';\" class=\"item_description_moreinfo\" id=\"basket_item_hidelink_". $index ."\" title=\"Expand the info on $title\">... mehr Info</a>";
    }
    return utf8_encode($currentstring);
}
?>